package com.example.demo.service;

import com.example.demo.mapper.TorrentMapper;
import com.example.demo.model.Torrent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PromotionService {
    private final TorrentMapper torrentMapper;

    @Autowired
    public PromotionService(TorrentMapper torrentMapper) {
        this.torrentMapper = torrentMapper;
    }

    @Transactional
    public void setupPromotion(Long torrentId, LocalDateTime startTime, LocalDateTime endTime) {
        // 设置促销时间
        torrentMapper.setPromotionTime(torrentId, startTime, endTime);

        // 如果立即生效
        if (startTime.isBefore(LocalDateTime.now())) {
            torrentMapper.activatePromotion(torrentId);
        }
    }

    public void closePromotion(Long torrentId){
        torrentMapper.deactivatePromotion(torrentId);
        torrentMapper.clearPromotionTime(torrentId);
    }

    public List<Torrent> getActivePromotions() {
        return torrentMapper.findActivePromotions();
    }
}